package com.siecom.module;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Base64;
import android.util.Log;

import com.ivsign.android.IDCReader.IDCReadInterface;
import com.siecom.bean.IdentityInfoBean;
import com.siecom.constconfig.ErrorDefine;
import com.siecom.hostservice.HKBaseApplication;
import com.siecom.hostservice.MainActivity;
import com.siecom.tool.ByteTool;
import com.siecom.tool.LogUtil;
import com.siecom.tool.Timer;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zhq on 2017/1/18.
 */

public class IdentityRunnable extends ModuleRunnable {

    public static IDCardModule identityModule = IDCardModule.getInstance();
    private final static String TAG = "IdentityRunnable";
    private boolean alwaysOpen = false;
    public static FinancialModule financialModule = FinancialModule.getInstance();
    private static ExecutorService threadPool = Executors.newFixedThreadPool(1);

    @Override
    public void stopRun() {
        this.runFlag = false;
        identityModule.setStopRun(true);
    }

    public IdentityRunnable(boolean alwaysOpen, final DeviceCallback callback, final int timeOutMs, final boolean isOnMain) {
        this.callback = callback;
        this.timeOutMs = timeOutMs;
        this.isOnMain = isOnMain;
        this.alwaysOpen = alwaysOpen;

    }

    /**
     * 关闭非接以免干扰
     */
    public void closePicc() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                financialModule.libPiccClose();
            }
        };
        threadPool.submit(runnable);

    }

    @Override
    public void run() {
        synchronized (this) {
            Timer tm = null;
            if (timeOutMs > 0) {
                tm = new Timer(timeOutMs);
                tm.start();
            }

//            IDCReadInterface.DLL_IDClose();
//            IDCReadInterface.DLL_IDOpen();
            if(Build.MODEL.equals("A108Plus")) {
                LogUtil.e(TAG,"IO转换 IDCReadInterface.gpio_control(100)");
                IDCReadInterface.gpio_control(100,0);
            }
            deliverResult(callback, DeviceInterface.Result.START, null, isOnMain);
            // 0键盘  1指纹 2IC卡  3磁条卡  5蓝牙 6 身份证
            MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)1);
            String[] sRet = new String[1];
            try {
                while (runFlag) {
                    if (tm.timeOut()) {
                        Log.e(TAG, "tm.timeOut!!");
                        Bundle bundle = new Bundle();
                        bundle.putInt("code", ErrorDefine.ID_TIMEOUT);
                        bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.ID_TIMEOUT));
                        deliverResult(callback, DeviceInterface.Result.ERROR, bundle, isOnMain);
                        break;
                    }

                    IdentityInfoBean bean = identityModule.ReadAllCardInfo(sRet);

                    if (bean != null) {
                        if(bean.icon!=null){
//                            bean.imageByte = Bitmap2Bytes(bean.icon);
//                            bean.imageStr =  ByteTool.byte2hex(bean.imageByte);
                            bean.imageByte = new byte[0];
                        }

                        LogUtil.e(TAG,"获取信息成功 deliver Success");
                        bean.icon = null;
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("data", bean);
                        deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
                        break;
                    } else {
//                        IDCReadInterface.DLL_IDClose();
//                        IDCReadInterface.DLL_IDOpen();

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
//                HKBaseApplication.saveCatchInfo2File(e);
            } finally {
//                IDCReadInterface.DLL_IDClose();

                isFinish = true;
                deliverResult(callback, DeviceInterface.Result.COMPLETED, new Bundle(), isOnMain);
                MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)0);
                SystemClock.sleep(100);

            }
        }

    }
    public static byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
}
